class_name EntityControllerComponent
extends ControllerComponent

@export var ghost_scene:PackedScene
@export var ghost_interval:float = 0.05
@onready var interactive_area_2d: Area2D = %InteractiveArea2D
var key_action_signal_dict:Dictionary = {}
var dodge_timer:Timer
var dodge_move_speed_scale_temp:float = 0
var cur_dodge_stamina_consume_per_second:float = 0
var ghost_timer:Timer
signal on_key_action(key_action:GameEnum.KeyAction,key_action_state:GameEnum.KeyActionState)

func init_component(entity):
	super.init_component(entity)
	for key_action in GameEnum.KeyAction.values():
		key_action_signal_dict[key_action] = {}
		key_action_signal_dict[key_action]["KeyActionState"] = GameEnum.KeyActionState.Idle
	on_key_action.connect(_on_key_action)
	ghost_timer = Timer.new()
	ghost_timer.name = "GhostTimer"
	add_child(ghost_timer)
	ghost_timer.autostart = false
	ghost_timer.one_shot = false
	ghost_timer.timeout.connect(_create_ghost)
	GameEventSystem.on_game_state_changed.connect(func (cur_game_state):
		reset_key_action()
		)
func press_key_action(key_action:GameEnum.KeyAction):
	if not key_action_signal_dict.has(key_action):
		return
	var item = key_action_signal_dict[key_action]
	if not item["KeyActionState"] == GameEnum.KeyActionState.Idle:
		return
	item["KeyActionState"] = GameEnum.KeyActionState.Pressed
	on_key_action.emit(key_action,item["KeyActionState"])
func release_key_action(key_action:GameEnum.KeyAction):
	if not key_action_signal_dict.has(key_action):
		return
	var item = key_action_signal_dict[key_action]
	if not item["KeyActionState"] == GameEnum.KeyActionState.Pressed and not item["KeyActionState"] == GameEnum.KeyActionState.Stay:
		return
	item["KeyActionState"] = GameEnum.KeyActionState.Release
	on_key_action.emit(key_action,item["KeyActionState"])
func stay_key_action(key_action:GameEnum.KeyAction):
	if not key_action_signal_dict.has(key_action):
		return
	var item = key_action_signal_dict[key_action]
	if not item["KeyActionState"] == GameEnum.KeyActionState.Pressed:
		return
	item["KeyActionState"] = GameEnum.KeyActionState.Stay
	on_key_action.emit(key_action,item["KeyActionState"])
func idle_key_action(key_action:GameEnum.KeyAction):
	if not key_action_signal_dict.has(key_action):
		return
	var item = key_action_signal_dict[key_action]
	if not item["KeyActionState"] == GameEnum.KeyActionState.Release:
		return
	item["KeyActionState"] = GameEnum.KeyActionState.Idle
	on_key_action.emit(key_action,item["KeyActionState"])
func get_target_vel() -> Vector2:
	return super.get_target_vel()
func set_target_vel(delta:float):
	super.set_target_vel(delta)
func reset_key_action():
	for key_action in GameEnum.KeyAction.values():
		release_key_action(key_action)
		idle_key_action(key_action)
func detect_action_keys():
	for key_action in GameEnum.KeyAction.values():
		stay_key_action(key_action)
func _on_key_action(key_action:GameEnum.KeyAction,key_action_state:GameEnum.KeyActionState):
	if key_action == GameEnum.KeyAction.Dodge and key_action_state == GameEnum.KeyActionState.Pressed:
		_dodge(true)
	if key_action == GameEnum.KeyAction.Dodge and key_action_state == GameEnum.KeyActionState.Release:
		_dodge(false)
func _create_ghost():
	var ghost:Ghost = GameEffectSystem.create_effect(ghost_scene,cur_entity.global_position) as Ghost
	ghost.init_ghost(cur_entity.sprite2d.duplicate(true))
func _dodge(is_start:bool):
	var health_component = cur_entity.get_component_by_name("HealthComponent") as HealthComponent
	if is_start:
		if dodge_timer:
			dodge_timer.stop()
			cur_entity.get_entity_data().move_speed_scale -= dodge_move_speed_scale_temp
			dodge_move_speed_scale_temp = 0
			cur_dodge_stamina_consume_per_second = 0
			dodge_timer.queue_free()
			dodge_timer = null
		var consume = cur_entity.get_entity_data().dodge_stamina_consume_per_second
		var duration = cur_entity.get_entity_data().dodge_duration
		var increase = cur_entity.get_entity_data().dodge_stamina_consume_increase_per_second
		cur_dodge_stamina_consume_per_second = consume
		if health_component.consume_stamina(consume * duration):
			_create_ghost()
			ghost_timer.start(ghost_interval)
			dodge_timer = Timer.new()
			dodge_timer.name = "DodgeTimer"
			add_child(dodge_timer)
			dodge_timer.one_shot = false
			health_component.set_stamina_is_consuming(true)
			dodge_timer.start(duration)
			dodge_timer.timeout.connect(func ():
				cur_dodge_stamina_consume_per_second += increase * duration
				if not health_component.consume_stamina(cur_dodge_stamina_consume_per_second * duration):
					_dodge(false)
				)
			dodge_move_speed_scale_temp = cur_entity.get_entity_data().dodge_move_speed_scale
			cur_entity.get_entity_data().move_speed_scale += dodge_move_speed_scale_temp
	else:
		health_component.set_stamina_is_consuming(false)
		ghost_timer.stop()
		if dodge_timer:
			dodge_timer.stop()
			cur_entity.get_entity_data().move_speed_scale -= dodge_move_speed_scale_temp
			dodge_move_speed_scale_temp = 0
			cur_dodge_stamina_consume_per_second = 0
			dodge_timer.queue_free()
			dodge_timer = null
func _process(delta: float) -> void:
	super._process(delta)
	detect_action_keys()
func _physics_process(delta: float) -> void:
	super._physics_process(delta)
